package com.shqcjd.sinopecdemomobile.ui.activity.problem;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.model.pojo.ProblemListBean;
import com.shqcjd.sinopecdemomobile.ui.adapter.ProblemListAdapter;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.SpacesItemDecoration;
import com.shqcjd.sinopecdemomobile.util.ToastUtils;
import com.shqcjd.sinopecdemomobile.util.UIUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 问题列表
 */
public class ProblemListActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener {

    private int mNextRequestPage = 1;
    private static final int PAGE_SIZE = 6;

    @BindView(R.id.iv_toolbar_back)
    ImageView ivToolbarBack;
    @BindView(R.id.tv_toolbar_center)
    TextView tvToolbarCenter;
    @BindView(R.id.et_search_problem)
    EditText etSearchProblem;
    @BindView(R.id.rv_problem_list)
    RecyclerView rvProblemList;
    @BindView(R.id.rl_feedback_btn)
    RelativeLayout rlFeedbackBtn;
    private View notDataView;
    private ProblemListAdapter problemListAdapter;
    private Context mContext;
    private String searchContent;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_problem_list;
    }

    @Override
    public void initView() {
        mContext = this;
        tvToolbarCenter.setText("问题描述");
        initProblemList();
    }

    private void initProblemList() {
        notDataView = getLayoutInflater().inflate(R.layout.layout_no_data, (ViewGroup) rvProblemList.getParent(), false);
        problemListAdapter = new ProblemListAdapter();
        rvProblemList.setLayoutManager(new LinearLayoutManager(this));
        rvProblemList.addItemDecoration(new SpacesItemDecoration(UIUtils.dip2px(10), UIUtils.dip2px(9)));
        rvProblemList.setAdapter(problemListAdapter);
        problemListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ProblemListBean.ListBean item = (ProblemListBean.ListBean) adapter.getItem(position);
                Intent intent = new Intent(mContext, ProblemDetailsActivity.class);
                intent.putExtra("problemFeedbackId",item.id);
                startActivity(intent);
            }
        });

        problemListAdapter.setOnLoadMoreListener(this,rvProblemList);


        etSearchProblem.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                searchContent = s.toString();
                refreshData();
            }
        });


    }

    @Override
    public void initData() {
        refreshData();
    }

    private void refreshData() {
        mNextRequestPage = 1;
        problemListAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        ApiService.problemList(mNextRequestPage,PAGE_SIZE,searchContent)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<ProblemListBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<ProblemListBean> retData) {
                        if (retData != null && retData.retCode == 10000) {
                            setData(true, retData.data.list);
                        } else {
                            ToastUtils.showShort(ProblemListActivity.this, retData.retMsg);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        problemListAdapter.setEnableLoadMore(true);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        problemListAdapter.setEnableLoadMore(true);
                    }
                });
    }

    private void setData(boolean isRefresh, List data) {

        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            if (size > 0) {
                problemListAdapter.setNewData(data);
            } else {
                problemListAdapter.setNewData(null);
                problemListAdapter.setEmptyView(notDataView);
            }
        } else {
            if (size > 0) {
                problemListAdapter.addData(data);
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            problemListAdapter.loadMoreEnd(isRefresh);
        } else {
            problemListAdapter.loadMoreComplete();
        }
    }

    private void loadMore() {
        ApiService.problemList(mNextRequestPage,PAGE_SIZE,searchContent)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<ProblemListBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<ProblemListBean> retData) {
                        if (retData != null && retData.retCode == 10000) {
                            boolean isRefresh =mNextRequestPage ==1;
                            setData(isRefresh, retData.data.list);
                        } else {
                            ToastUtils.showShort(ProblemListActivity.this, retData.retMsg);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        problemListAdapter.loadMoreFail();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    @OnClick({R.id.iv_toolbar_back, R.id.rl_feedback_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_toolbar_back:
                finish();
                break;
            case R.id.rl_feedback_btn://问题反馈
                startActivity(new Intent(mContext, ProblemFeedbackActivity.class));
                break;
        }
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }
}
